/**
 * Created by yuming619 on 2017/10/20.
 */
/**
 * Created by Xiaoming on 2017/9/28.
 */
angular.module('module.lock')
    .controller('LockController', LockController);
// 用户优惠券列表
LockController.$inject = ['$rootScope', '$state', '$stateParams', 'CupboarlockService', '$interval', '$scope', '$cookies', 'IMSdkService', 'MsgmqhpService'];
function LockController($rootScope, $state, $stateParams, CupboarlockService, $interval, $scope, $cookies, IMSdkService, MsgmqhpService) {
    var vm = this;
    vm.lock = "正在开锁中......";
    //$state.go("scan",{shopid:$stateParams.shopid,appid:$stateParams.lock_appid});
    //查询门锁状态

    /*var cupboard = {
     devid: '12011',
     act: 1,
     num: '12011',
     userid: $cookies.get("_userId"),
     //userid: 5,
     };*/
    var cupboard = {
        devid: '50051',
        act: 1,
        num: 1,
        time: 5,
        userid: $cookies.get("_userId"),
        //userid: 5,
        "volu": 75,
        "openvo": "开门成功 欢迎使用",
        "timevo": "门已关 谢谢使用",
        "clovo": "门已关 谢谢使用"
    };
    if ($stateParams.devid) {
        cupboard.devid = $stateParams.devid;
    }
    var _time;
    vm.$onInit = function () {
        vm.lockStatus = 1;
    };
    //$state.go("scan",{shopid:$stateParams.shopid,appid:$stateParams.lock_appid});//测试
    if (cupboard.userid) {
        //创建一个用户会话
        MsgmqhpService.creatMqhp(cupboard.userid, cupboard.devid,function(error){
            alert("订阅消息失败！");
        });
        CupboarlockService.getstatus(cupboard).$promise.then(
            function (data) {
                //锁开着或者关着
                //判断设备状
                if (data.dBsnsta === "5" && data.dDevsta === '2') {
                    alert("设备处于掉线状态！");
                    vm.lock = "设备处于掉线状态！";
                } else if (data.dDevsta === '1') {
                    alert("该设备未注册！");
                    vm.lock = "该设备未注册！";
                }
                else if (data.dDevsta === '3') {
                    alert("该设备已经欠费!");
                    vm.lock = "该设备已经欠费！";
                }
                else {
                    check_user(data.lock, data.userid, 1);//带type
                }
            }),
            function (error) {
                alert("状态查询失败！")
            };
        IMSdkService.onChatMessage(function (fromtype, chatid, message) {
            check_user(message.body.act, message.body.userid);//不带type
        });
    } else {
        alert("请先登录！");
    }


///////////////////////////////////////////
    function check_user(status, userid, type) {
        switch (status) {
            case 1:
                if (cupboard.userid != userid) {
                    apply_status("请等待！有人正在使用...", 1, type);
                } else {
                    apply_status("点击扫描", 2, type);
                }
                break;
            case 2:
            case 5://开门超时
            case 3://开锁异常
            case 4://关锁异常
                if (!type && cupboard.userid == userid) {
                    apply_status("准备结算",3,type);//监听到
                } else {
                    CupboarlockService.updatestatus(cupboard, function (success) {
                        console.log(success);
                        if (success.status == 201) {
                            apply_status("点击扫描", 2, type);
                        }
                    }, function (error) {
                        //;/("开门失败！");
                        apply_status(error.message, 1, type);
                    });
                }
                break;
        }
    }

    function apply_status(lock, lockStatus, type) {
        //自带更新，不用apply
        if (type) {
            vm.lock = lock;
            vm.lockStatus = lockStatus;
        } else {
            $scope.$apply(function () {
                vm.lock = lock;
                vm.lockStatus = lockStatus;
            })
        }
    }
}